package com.shfl.a;

import java.io.*;
import java.util.LinkedList;
import java.util.List;

public class Cat implements Serializable {


    private String name;

    public Cat () {

        this.name = "new cat";

    }

    public String getName() {

        return this.name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public static void main(String[] args) {

        Cat cat = new Cat();
        List<Cat> catLst = new LinkedList<>();
        try {

            FileOutputStream fos = new FileOutputStream("catDemo.txt");

            ObjectOutputStream oos = new ObjectOutputStream(fos);

            System.out.println(" 1> " + cat.getName());

            cat.setName("My Cat");
            catLst.add(cat);
            catLst.add(cat);
            oos.writeObject(catLst);

            oos.close();

        } catch (Exception ex) {  ex.printStackTrace();   }

        try {

            FileInputStream fis = new FileInputStream("catDemo.txt");

            ObjectInputStream ois = new ObjectInputStream(fis);

            catLst = (List<Cat>) ois.readObject();

            catLst.stream().forEach(it->{
                System.out.println(it.getName());
            });
           // System.out.println(" 2> " + cat.getName());

            ois.close();

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }

}
